What is remark-stringify?
The remark-stringify package is a plugin for the remark ecosystem that allows you to serialize Markdown Abstract Syntax Trees (ASTs) into Markdown text. It is commonly used in conjunction with other remark plugins to process and transform Markdown content programmatically.
What are remark-stringify's main functionalities?
Basic Markdown Serialization
This feature allows you to convert a Markdown AST into a Markdown string. The example demonstrates how to serialize a simple AST representing a paragraph with the text 'Hello, world!'.
const remark = require('remark');
const stringify = require('remark-stringify');
const markdownAST = {
type: 'root',
children: [
{ type: 'paragraph', children: [{ type: 'text', value: 'Hello, world!' }] }
]
};
const markdownText = remark().use(stringify).stringify(markdownAST);
console.log(markdownText); // Outputs: 'Hello, world!\n'
Customizing Output
This feature allows you to customize the output format of the serialized Markdown. The example shows how to set options for bullet points and fenced code blocks.
const remark = require('remark');
const stringify = require('remark-stringify');
const markdownAST = {
type: 'root',
children: [
{ type: 'paragraph', children: [{ type: 'text', value: 'Hello, world!' }] }
]
};
const markdownText = remark()
.use(stringify, { bullet: '*', fences: true })
.stringify(markdownAST);
console.log(markdownText); // Outputs: 'Hello, world!\n'
Handling Complex ASTs
This feature demonstrates how to handle more complex ASTs, including headings, paragraphs, and lists. The example shows how to serialize an AST with multiple types of nodes.
const remark = require('remark');
const stringify = require('remark-stringify');
const markdownAST = {
type: 'root',
children: [
{ type: 'heading', depth: 1, children: [{ type: 'text', value: 'Title' }] },
{ type: 'paragraph', children: [{ type: 'text', value: 'This is a paragraph.' }] },
{ type: 'list', ordered: false, children: [
{ type: 'listItem', children: [{ type: 'paragraph', children: [{ type: 'text', value: 'Item 1' }] }] },
{ type: 'listItem', children: [{ type: 'paragraph', children: [{ type: 'text', value: 'Item 2' }] }] }
] }
]
};
const markdownText = remark().use(stringify).stringify(markdownAST);
console.log(markdownText); // Outputs: '# Title\n\nThis is a paragraph.\n\n* Item 1\n* Item 2\n'
Other packages similar to remark-stringify
markdown-it
Markdown-it is a fast and flexible Markdown parser that can also be used to render Markdown from an AST. It offers a wide range of plugins and customization options, making it a versatile alternative to remark-stringify.
marked
Marked is a low-level Markdown compiler that allows for extensive customization. It is known for its speed and flexibility, making it a good choice for projects that require fine-grained control over Markdown rendering.
showdown
Showdown is a bidirectional Markdown to HTML converter that can also be used to serialize Markdown from an AST. It is easy to use and offers a range of extensions for additional functionality.
Compiler for unified.
Serializes mdast syntax trees to Markdown.
Used in the remark processor but can be used on its own as well.
Can be extended to change how Markdown is serialized.
Install
npm:
npm install remark-stringify
Use
var unified = require('unified')
var createStream = require('unified-stream')
var html = require('rehype-parse')
var rehype2remark = require('rehype-remark')
var stringify = require('remark-stringify')
var processor = unified().use(html).use(rehype2remark).use(stringify, {
bullet: '*',
fence: '~',
fences: true,
incrementListMarker: false
})
process.stdin.pipe(createStream(processor)).pipe(process.stdout)
See unified for more examples »
API
See unified for API docs »
processor().use(stringify[, options])
Configure the processor
to serialize mdast syntax trees to
Markdown.
options
Options can be passed directly, or passed later through
processor.data()
.
All the formatting options of mdast-util-to-markdown
are supported and will be passed through.
Extending the compiler
See mdast-util-to-markdown
.
Then create a wrapper plugin such as remark-gfm
.
Security
remark-stringify
will do its best to serialize markdown to match the syntax
tree, but there are several cases where that is impossible.
It’ll do its best, but complete roundtripping is impossible given that any
value could be injected into the tree.
As Markdown is sometimes used for HTML, and improper use of HTML can open you up
to a cross-site scripting (XSS) attack, use of remark-stringify
and
parsing it again later can potentially be unsafe.
When parsing Markdown afterwards, use remark in combination with the
rehype ecosystem, and use rehype-sanitize
to make
the tree safe.
Use of remark plugins could also open you up to other attacks.
Carefully assess each plugin and the risks involved in using them.
Contribute
See contributing.md
in remarkjs/.github
for ways
to get started.
See support.md
for ways to get help.
Ideas for new plugins and tools can be posted in remarkjs/ideas
.
A curated list of awesome remark resources can be found in awesome
remark.
This project has a code of conduct.
By interacting with this repository, organization, or community you agree to
abide by its terms.
Support this effort and give back by sponsoring on OpenCollective!
License
MIT © Titus Wormer